package com.findfuture.demo.service.impl;

import com.findfuture.common.dao.HibernateDaoImpl;
import com.findfuture.demo.model.Albums;
import com.findfuture.demo.model.Users;
import com.findfuture.demo.service.IAlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Alex
 * Date: 11-12-14
 * Time: 下午9:17
 * To change this template use File | Settings | File Templates.
 */
@Service("albumService")
public class AlbumServiceImpl implements IAlbumService{

    @Autowired
    private HibernateDaoImpl commonDao;

    public List<Albums> getAlbumsList() throws Exception{
        String hql = " from " + Albums.class.getSimpleName();
        commonDao.setEntryClass(Users.class);
        return commonDao.query(hql,null);
    }

    @Override
    public void addAlbum(Albums album) throws Exception{
        commonDao.setEntryClass(Albums.class);
        commonDao.save(album);
    }

    @Override
    public Albums getAlbumsById(int id) throws Exception {
        commonDao.setEntryClass(Albums.class);
        return (Albums) commonDao.fetch(id);
    }

    @Override
    public void updateAlbumName(int id, String name) throws Exception {
        commonDao.setEntryClass(Albums.class);
        commonDao.execute("update " + Albums.class.getSimpleName() + " set name = ? where id = ?" , name , id);
    }

    @Override
    public void deleteAlbum(int id) throws Exception {
        commonDao.setEntryClass(Albums.class);
        commonDao.execute("delete from " + Albums.class.getSimpleName() + " where id = " + id , null);
    }
}
